<script setup>
import {
    UserFilled,
    User,
    EditPen,
    SwitchButton,
    CaretBottom,
    Lock,
    Star,
    MessageBox
} from '@element-plus/icons-vue'
import { onMounted } from 'vue';
import avatar from '@/assets/user.png'
import { userInfoService } from '@/api/user.js';
import useUserInfoStore from '@/stores/userinfo.js'
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router'
const router = useRouter()

//使用pinia
const useInfoStore = useUserInfoStore();

//调用函数，获取用户的详细信息
const getUserInfo = async () => {
    try {
        //调用接口
        let result = await userInfoService();
        //存储数据到pinia中
        useInfoStore.setInfo(result.data);
    } catch {
        ElMessage.error('服务异常')
    }
}

const handleCommande = (command) => {
    if (command === 'info') {
        router.push('/user/info') // 替换为你的基本资料页面路径
    } else if (command === 'resetPassword') {
        router.push('/user/ResetPassword') // 替换为你的重置密码页面路径
    } else if (command === 'logout') {
        ElMessage.success('修改密码成功！正在转到登录界面...');

        //跳转到登录页面
        router.push('/');
        ElMessage({
            type: 'success',
            message: '退出登录成功',
        });
    }
}


// 使用生命周期钩子
onMounted(async () => {
    await getUserInfo();
});

</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!-- 菜单标签 -->
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-menu-item index="/char/SummaryData">
                    <el-icon>
                        <MessageBox />
                    </el-icon>
                    <span style="font-size: 16px;">图表分析</span>
                </el-menu-item>
                <el-menu-item index="/char/ForcasteData">
                    <el-icon>
                        <Star />
                    </el-icon>
                    <span style="font-size: 16px;">未来预测</span>
                </el-menu-item>
                <!-- 子菜单 -->
                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span style="font-size: 16px;">个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/ResetPassword">
                        <el-icon>
                            <Lock />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>管理员：<strong>{{ useInfoStore.info?.username || '加载中...' }}</strong></div>
                <div><span style="font-size: 21px;">北京上海电力分析一览图</span></div>
                <!-- 下拉菜单 -->
                <el-dropdown placement="bottom-end" @command="handleCommande">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>
                <div class="footer-text">北京上海电力分析一览图系统©2025 Created by rainbow</div>
                <div class="heart" @click="handleHeartClick">❤️</div>
                <!-- 悬浮文字 -->
                <div v-if="showFloatingText" class="floating-text">zsy ❤️ hyj</div>
            </el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 50px;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;

        .footer-text {
            text-align: center;
        }

        .heart {
            cursor: pointer;
            transition: transform 0.3s ease, color 0.3s ease;
            user-select: none;
            /* 禁止文本选中 */
        }

        .heart:hover {
            color: #ff69b4;
            transform: scale(1.1);
        }

        .heart-animation {
            animation: bounce 0.3s ease;
        }

        @keyframes bounce {
            0% {
                transform: scale(1);
            }

            50% {
                transform: scale(1.2);
            }

            100% {
                transform: scale(1);
            }
        }
    }

    .floating-text {
        position: absolute;
        top: -30px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 16px;
        color: red;
        animation: floatUp 3s ease forwards;
    }

    @keyframes floatUp {
        0% {
            opacity: 1;
            transform: translate(-50%, 0);
        }

        100% {
            opacity: 0;
            transform: translate(-50%, -50px);
        }
    }
}
</style>
